﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace LibBot
{
    public class HoNEntityManager
    {
        private Hashtable collEntities;
        private Hashtable _collEntities;

        public HoNEntityManager()
        {
            this.collEntities = new Hashtable();
        }

        public void AddEntity(string Entity, UInt32 EntityID, byte flags, List<char> bot_flags)
        {
            if (!collEntities.ContainsKey(Entity))
            {
                this._collEntities = new Hashtable();
                _collEntities.Add("userid", EntityID);
                _collEntities.Add("flags", flags);
                _collEntities.Add("bot_flags", bot_flags);
                collEntities.Add(Entity, _collEntities);
            }
        }

        public UInt32 GetEntityIDFromEntity(string Entity)
        {
            XmlDocument xmlDoc = new XmlDocument();
            Hashtable info;

            foreach (string key in collEntities.Keys)
            {
                info = (Hashtable)collEntities[key];

                if (key.Contains("]") && !Entity.Contains("]"))
                {
                    if (key.Substring(key.IndexOf("]") + 1) == Entity)
                    {
                        return Convert.ToUInt32(info["userid"]);
                    }
                }
                else if (key == Entity)
                {
                    return Convert.ToUInt32(info["userid"]);
                }
            }

            xmlDoc.Load(@"http://xml.heroesofnewerth.com/xml_requester.php?f=nick2id&opt=nick&nick[]=" + Entity);
            AddEntity(Entity, Convert.ToUInt32(xmlDoc["xmlRequest"]["accounts"]["account_id"].InnerText), 0, new List<char> { });
            return Convert.ToUInt32(xmlDoc["xmlRequest"]["accounts"]["account_id"].InnerText);
        }

        public string GetEntityFromEntityID(UInt32 EntityID)
        {
            XmlDocument xmlDoc = new XmlDocument();

            foreach (Hashtable ht in collEntities.Values)
            {
                if (ht.ContainsValue(EntityID))
                {
                    Hashtable info;

                    foreach (string key in collEntities.Keys)
                    {
                        info = (Hashtable)collEntities[key];

                        if (Convert.ToUInt32(info["userid"]) == EntityID)
                        {
                            return key;
                        }
                    }
                }
            }

            xmlDoc.Load(@"http://xml.heroesofnewerth.com/xml_requester.php?f=id2nick&opt=aid&aid[]=" + EntityID);
            AddEntity(xmlDoc["xmlRequest"]["accounts"]["nickname"].InnerText, EntityID, 0, new List<char> { });
            return xmlDoc["xmlRequest"]["accounts"]["nickname"].InnerText;
        }

        public byte GetFlagsFromEntity(string Entity)
        {
            Hashtable info;

            foreach (string key in collEntities.Keys)
            {
                info = (Hashtable)collEntities[key];

                if (key.Contains("]") && !Entity.Contains("]"))
                {
                    if (key.Substring(key.IndexOf("]") + 1) == Entity)
                    {
                        return (byte)info["flags"];
                    }
                }
                else if (key == Entity)
                {
                    return (byte)info["flags"];
                }
            }

            return 0;
        }

        public byte GetFlagsFromEntityID(UInt32 EntityID)
        {
            foreach (Hashtable ht in collEntities.Values)
            {
                if (ht.ContainsValue(EntityID))
                {
                    Hashtable info;

                    foreach (string key in collEntities.Keys)
                    {
                        info = (Hashtable)collEntities[key];

                        if (Convert.ToUInt32(info["userid"]) == EntityID)
                        {
                            return (byte)info["flags"];
                        }
                    }
                }
            }

            return 0;
        }

        public List<char> GetBotFlagsFromEntity(string Entity)
        {
            Hashtable info;

            foreach (string key in collEntities.Keys)
            {
                info = (Hashtable)collEntities[key];

                if (key.Contains("]") && !Entity.Contains("]"))
                {
                    if (key.Substring(key.IndexOf(']') + 1) == Entity)
                    {
                        return (List<char>)info["bot_flags"];
                    }
                }
                else if (key == Entity)
                {
                    return (List<char>)info["bot_flags"];
                }
            }

            return null;
        }

        public List<char> GetBotFlagsFromEntityID(UInt32 EntityID)
        {
            foreach (Hashtable ht in collEntities.Values)
            {
                if (ht.ContainsValue(EntityID))
                {
                    Hashtable info;

                    foreach (string key in collEntities.Keys)
                    {
                        info = (Hashtable)collEntities[key];

                        if (Convert.ToUInt32(info["userid"]) == EntityID)
                        {
                            return (List<char>)info["bot_flags"];
                        }
                    }
                }
            }

            return null;
        }

        public void UpdateFlags(string Entity, byte Flags)
        {
            foreach (string key in collEntities.Keys)
            {
                if (key.Contains("]") && !Entity.Contains("]"))
                {
                    if (key.Substring(key.IndexOf("]") + 1) == Entity)
                    {
                        ((Hashtable)collEntities[key])["flags"] = Flags;
                        return;
                    }
                }
                else if (key == Entity)
                {
                    ((Hashtable)collEntities[key])["flags"] = Flags;
                    return;
                }
            }
        }

        public void UpdateFlags(UInt32 EntityID, byte Flags)
        {
            foreach (Hashtable ht in collEntities.Values)
            {
                if (ht.ContainsValue(EntityID))
                {
                    foreach (string key in collEntities.Keys)
                    {
                        if (Convert.ToUInt32(((Hashtable)collEntities[key])["userid"]) == EntityID)
                        {
                            ((Hashtable)collEntities[key])["flags"] = Flags;
                            return;
                        }
                    }
                }
            }
        }

        public void AddBotFlags(string Entity, List<char> BotFlags)
        {
            foreach (string key in collEntities.Keys)
            {
                if (key.Contains("]") && !Entity.Contains("]"))
                {
                    if (key.Substring(key.IndexOf("]") + 1) == Entity)
                    {
                        foreach (char flag in BotFlags)
                        {
                            if (!((List<char>)((Hashtable)collEntities[key])["bot_flags"]).Contains(flag))
                            {
                                ((List<char>)((Hashtable)collEntities[key])["bot_flags"]).Add(flag);
                            }
                        }

                        return;
                    }
                }
                else if (key == Entity)
                {
                    foreach (char flag in BotFlags)
                    {
                        if (!((List<char>)((Hashtable)collEntities[key])["bot_flags"]).Contains(flag))
                        {
                            ((List<char>)((Hashtable)collEntities[key])["bot_flags"]).Add(flag);
                        }
                    }

                    return;
                }
            }
        }

        public void AddBotFlags(UInt32 EntityID, List<char> BotFlags)
        {
            foreach (Hashtable ht in collEntities.Values)
            {
                if (ht.ContainsValue(EntityID))
                {
                    foreach (string key in collEntities.Keys)
                    {
                        if (Convert.ToUInt32(((Hashtable)collEntities[key])["userid"]) == EntityID)
                        {
                            foreach (char flag in BotFlags)
                            {
                                if (!((List<char>)((Hashtable)collEntities[key])["bot_flags"]).Contains(flag))
                                {
                                    ((List<char>)((Hashtable)collEntities[key])["bot_flags"]).Add(flag);
                                }
                            }

                            return;
                        }
                    }
                }
            }
        }

        public void RemoveEntity(string Entity)
        {
            if (collEntities.ContainsKey(Entity))
            {
                collEntities.Remove(Entity);
            }
        }

        public int CountEntities()
        {
            return collEntities.Count;
        }
    }
}
